<?php
/**
 *  Create On 2014-12-06
 *  Author yiwei
 *  QQ:1006629314
 **/
class PushController extends Controller{
	
	private $logger;
	private $send_mes;
	
	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/clientapi.php";
		$this->logger = LoggerManager::getLogger('Pushlog');
		$this->send_mes=$this->getRequest()->hostUrl. "/common/clientapi/images/50.png";
	}
	
	/**
	 * 使用shell命令crontab完成定时推送
	 * 每5分钟执行以下请求命令如下
	 *"*\/5 * * * * curl http://localhost/Push/execPushTask/minute/5"(中间的反斜杠多余)
	 **/
	
	//执行推送任务
	public function execPushTask(){
		$start = $this->getRequest()->get('start')?$this->getRequest()->get('start'):date('Y-m-d H:i:s');
		$minute = $this->getRequest()->get('minute');
		$homework = new homework();
		$period = new period();
		$homework_period = new homework_period();
		$class_student = new class_student();
		//var_dump($start);
		$list = $homework->getHomeworkForPush($start, $minute);
		$this->logger->debug($start.' @'.$minute.':task start');
		if($list){
			foreach ($list as $key=>$val){
				$id = $homework_period->publishHomeworkPeriod($val['id']);
				$this->logger->debug('publish:@id:'.$id);
				$result = $period->getPeriodById($val['period_id']);
				//var_dump($result);
				$targetList = $class_student->getStudentInfoByClass($result['classId']);
				$stu_list = $class_student->getStudentIdByPeriod($val['period_id']);
				$message_list = $this->getApp()->loadConf("message");
				$message = new message();
				foreach ($stu_list as $keys=>$vals){
					$message->addMessage(21,$val['homework_id'],$message_list['new_homework'],0,$stu_list[$keys]['student_id']);
				}
				
				$text = '你有一项新布置的作业';
				$content = array(
						"info"=>'trans test透传',
						"title"=>'UDO学生端',
						"text"=>$text,
						"logo_url"=>$this->send_mes,
						
				);
				if($targetList){
					foreach ($targetList as $val){
						//var_dump($val);
						$rep = $this->push->pushToSingle($val, $content);
						$this->logger->info('to:'.$val['name'].',result:'.implode(';',$rep));
					}
				}
			}
		}
		
		//$this->_pushHomework($start, $minute);		
	}
	
	
}